using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Pyramid
{
    class Chaikin:Multiresolution
    {
        public override ImageWithDetail DownSample(ImageWithDetail dimage)
        {
            int i, j;
            double[] r;
            GImage image = dimage.TheImage;
            GImage FinalImage = new GImage(image.Width / 2, image.Height / 2);

            //decrease the resolution in horizontal direction
            GImage RowImage = new GImage(image.Width / 2, image.Height);
            for (i = 0; i < image.Width; i += 2)
                for (j = 0; j < image.Height; j++)
                {

                    r = Find_NewValue(image, new double[4] {-0.25, 0.75, 0.75, -0.25}, new int[2] { i, j },0);
                    RowImage.SetPixel(i / 2, j, r);
                }

            //decrease the resolution in vertical direction
            for (i = 0; i < RowImage.Width; i++)
                for (j = 0; j < RowImage.Height; j += 2)
                {
                    r = Find_NewValue(RowImage, new double[4] { -0.25, 0.75, 0.75, -0.25 }, new int[2] { i, j }, 1);
                    FinalImage.SetPixel(i, j / 2, r);


                }
            dimage.TheImage = FinalImage;
            return dimage;
        }
        public override ImageWithDetail UpSample(ImageWithDetail dimage)
        {
            int i, j;
            double[] r;
            GImage image = dimage.TheImage;
            GImage FinalImage = new GImage(image.Width * 2, image.Height * 2);

            //increase the resolution in horizontal direction
            GImage RowImage = new GImage(image.Width * 2, image.Height);
            for (i = 0; i < image.Width; i++)
                for (j = 0; j < image.Height; j++)
                {

                    r = Find_NewValue(image, new double[2] { 0.75,0.25 }, new int[2] { i, j }, 0);
                    RowImage.SetPixel(2 * i, j, r);

                    r = Find_NewValue(image, new double[2] { 0.25,0.75 }, new int[2] { i, j }, 0);
                    RowImage.SetPixel(2 * i + 1, j, r);
                }

            //increase the resolution in vertical direction
            for (i = 0; i < RowImage.Width; i++)
                for (j = 0; j < RowImage.Height; j++)
                {
                    r = Find_NewValue(RowImage, new double[2] { 0.75, 0.25 }, new int[2] { i, j }, 1);
                    FinalImage.SetPixel(i, 2 * j,r);

                    r = Find_NewValue(RowImage, new double[2] { 0.25, 0.75 }, new int[2] { i, j }, 1);
                    FinalImage.SetPixel(i, 2 * j + 1, r);
                }
            dimage.TheImage = FinalImage;
            return dimage;
        }
    }
}
